<?php

/**
 * This is the model class for table "alt_user".
 *
 * The followings are the available columns in table 'alt_user':
 * @property integer $id
 * @property string $first_name
 * @property string $middle_name
 * @property string $last_name
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $picture
 * @property string $birthday
 * @property string $country
 * @property string $address
 * @property string $tele
 * @property string $fax
 * @property string $web
 * @property string $status
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
    
         public $new_password;
         public $repeat_password;
         


         public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'alt_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, email,first_name,', 'required'),
			array('first_name, middle_name, last_name, password, email', 'length', 'max'=>255),
			array('tele, fax', 'numerical', 'integerOnly'=>true, 'min'=>0),
                        array('email', 'unique', 'className'=>'user', 'attributeName'=>'email','message'=>'This email already exists.'),
                       	array('birthday, address, web,country', 'safe'),
                        array('picture', 'file', 'types'=>'jpg, gif, png,jpeg','allowEmpty'=>true),
			array('id, first_name, middle_name, last_name, username, password, email, picture, birthday, country, address, tele, fax, web, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'first_name' => 'First Name',
			'middle_name' => 'Middle Name',
			'last_name' => 'Last Name',
			'username' => 'Username',
                        'new_password' => 'New Password',
                        'repeat_password' => 'Repeat Password',
                        'assignItem'=>'Assign Item',
			'password' => 'Password',
			'email' => 'Email',
			'picture' => 'Picture',
			'birthday' => 'Birthday',
			'country' => 'Country',
			'address' => 'Address',
			'tele' => 'Tele',
			'fax' => 'Fax',
			'web' => 'Web',
			'status' => 'Status',
		);
	}

	
	public function search()
	{
		
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('first_name',$this->first_name,true);
		$criteria->compare('middle_name',$this->middle_name,true);
		$criteria->compare('last_name',$this->last_name,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('picture',$this->picture,true);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('country',$this->country,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('tele',$this->tele,true);
		$criteria->compare('fax',$this->fax,true);
		$criteria->compare('web',$this->web,true);
		$criteria->compare('status',$this->status,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        
    public function encryptPassword($password) {
        return md5(md5($password) . Yii::app()->params['passwordSalt']);
    }

    public function validatePassword($password) {
        return $this->encryptPassword($password) == $this->password;
    }
    
        

}